#include "IO/PCFileSystem.h"

#include "Framework/StableHeaders.h"

namespace tyro
{
	namespace IO
	{

		//-----------------------------------------------------------------------------
		// Open file and return
		// If read set to true data will not be cleared
		//-----------------------------------------------------------------------------
		IFile* PCFileSystem::Open(const char *filename, bool read) const
		{
			IFile* file = new IFile();
			file->Open(filename,read);
			return file;
		}

		//-----------------------------------------------------------------------------
		// Open file and append with string
		//-----------------------------------------------------------------------------
		bool PCFileSystem::Append(const char* filename, const char* const text, ...) const
		{
			IFile file;
			file.OpenAppend(filename);
			unsigned int bytesWrote = file.WriteBytes(text, strlen(text));
			file.Close();
			return bytesWrote != 0;
		}


		//-----------------------------------------------------------------------------
		// Create File
		//-----------------------------------------------------------------------------
		bool PCFileSystem::Create(const char* const fileName) const
		{
			IFile file;
			file.Open(fileName, false);
			bool result = file.IsOpen();
			file.Close();
			return result;
		}

		//-----------------------------------------------------------------------------
		// Delete File
		//-----------------------------------------------------------------------------
		bool PCFileSystem::Delete(const char* const fileName) const
		{
			
			return remove(fileName) != 0; 
		}

		//-----------------------------------------------------------------------------
		// Read bytes from file
		//-----------------------------------------------------------------------------
		unsigned int PCFileSystem::Read(IFile *file, char *&buffer, unsigned int bytesToRead, bool allocate) const
		{
			/* If allocation of buffer is required initise buffer size */
			if(allocate)
			{
				buffer = new char[bytesToRead];
			}

			return file->ReadBytes(buffer, bytesToRead);
		}

		//-----------------------------------------------------------------------------
		// Write bytes to file
		//-----------------------------------------------------------------------------
		unsigned int PCFileSystem::Write(tyro::IO::IFile *file, char *&buffer, unsigned int bytesToWrite) const
		{
			return file->WriteBytes(buffer, bytesToWrite);
		}

		//-----------------------------------------------------------------------------
		// Close the file
		//-----------------------------------------------------------------------------
		void PCFileSystem::Close(tyro::IO::IFile *file) const
		{
			file->Close();
		}

		//-----------------------------------------------------------------------------
		// Check if a file exists
		//-----------------------------------------------------------------------------
		bool PCFileSystem::Exists(const char* const fileName) const
		{
			/* Attempt to open the file */
			IFile file;
			file.Open(fileName, true);
			bool attempt = file.IsOpen();
			file.Close();
			return attempt;
		}

		//-----------------------------------------------------------------------------
		// Get file length
		//-----------------------------------------------------------------------------
		UINT_32 PCFileSystem::GetFileLength(const char* const fileName) const
		{
			/* Attempt to open the file */
			IFile file;
			file.Open(fileName, false);

			UINT_32 length = file.Length();

			file.Close();

			return length;
		}

	}
}